Skip to main content

第 5 章:自動擴縮集群 DNS 服務

如何在 Kubernetes 集群中啟用和配置 DNS 服務的自動擴縮功能

準備開始

  • 你必須擁有一個 Kubernetes 的集群,同時你必須配置 kubectl 命令行工具與你的集群通信。 建議在至少有兩個不作為控制平面主機的節點的集群上運行本教程。 如果你還沒有集群,你可以通過 Minikube 構建一個你自己的集群,或者你可以使用下面的 Kubernetes 練習環境之一:要獲知版本信息,請輸入 kubectl version.
  • 本指南假設你的節點使用 AMD64 或 Intel 64 CPU 架構
  • 確保 Kubernetes DNS 已啟用。

確定是否 DNS 水平自動擴縮特性已經啟用

在 kube-system 命名空間中列出集群中的 Deployment

kubectl get deployment --namespace=kube-system

NAME                      READY   UP-TO-DATE   AVAILABLE   AGE
...
dns-autoscaler 1/1 1 1 ...
...

如果在輸出中看到 “dns-autoscaler”,說明 DNS 水平自動擴縮已經啟用, 可以跳到調優 DNS 自動擴縮參數

獲取 DNS Deployment 的名稱

列出集群內 kube-system 命名空間中的 DNS Deployment:

kubectl get deployment -l k8s-app=kube-dns --namespace=kube-system

NAME      READY   UP-TO-DATE   AVAILABLE   AGE
...
coredns 2/2 2 2 ...
...

如果看不到 DNS 服務的 Deployment,你也可以通過名字來查找:

kubectl get deployment --namespace=kube-system

並在輸出中尋找名稱為 coredns 或 kube-dns 的 Deployment。

你的擴縮目標為:

Deployment/<your-deployment-name>

其中 <your-deployment-name> 是 DNS Deployment 的名稱。 例如,如果你的 DNS Deployment 名稱是 coredns,則你的擴展目標是 Deployment/coredns。

說明: CoreDNS 是 Kubernetes 的默認 DNS 服務。CoreDNS 設置標簽 k8s-app=kube-dns, 以便能夠在原來使用 kube-dns 的集群中工作。

啟用 DNS 水平自動擴縮

在本節,我們創建一個新的 Deployment。Deployment 中的 Pod 運行一個基於 cluster-proportional-autoscaler-amd64 鏡像的容器。

dns-horizontal-autoscaler.yaml
kind: ServiceAccount
apiVersion: v1
metadata:
name: kube-dns-autoscaler
namespace: kube-system
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: system:kube-dns-autoscaler
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["list", "watch"]
- apiGroups: [""]
resources: ["replicationcontrollers/scale"]
verbs: ["get", "update"]
- apiGroups: ["apps"]
resources: ["deployments/scale", "replicasets/scale"]
verbs: ["get", "update"]
# 待以下 issue 修復後,請刪除 Configmaps
# kubernetes-incubator/cluster-proportional-autoscaler#16
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get", "create"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: system:kube-dns-autoscaler
subjects:
- kind: ServiceAccount
name: kube-dns-autoscaler
namespace: kube-system
roleRef:
kind: ClusterRole
name: system:kube-dns-autoscaler
apiGroup: rbac.authorization.k8s.io

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: kube-dns-autoscaler
namespace: kube-system
labels:
k8s-app: kube-dns-autoscaler
kubernetes.io/cluster-service: "true"
spec:
selector:
matchLabels:
k8s-app: kube-dns-autoscaler
template:
metadata:
labels:
k8s-app: kube-dns-autoscaler
spec:
priorityClassName: system-cluster-critical
securityContext:
seccompProfile:
type: RuntimeDefault
supplementalGroups: [ 65534 ]
fsGroup: 65534
nodeSelector:
kubernetes.io/os: linux
containers:
- name: autoscaler
image: registry.k8s.io/cpa/cluster-proportional-autoscaler:1.8.4
resources:
requests:
cpu: "20m"
memory: "10Mi"
command:
- /cluster-proportional-autoscaler
- --namespace=kube-system
- --configmap=kube-dns-autoscaler
# 應該保持目標與 cluster/addons/dns/kube-dns.yaml.base 同步。
- --target=<SCALE_TARGET>
# 當集群使用大節點(有更多核)時,“coresPerReplica”應該占主導地位。
# 如果使用小節點,“nodesPerReplica“ 應該占主導地位。
- --default-params={"linear":{"coresPerReplica":256,"nodesPerReplica":16,"preventSinglePointFailure":true,"includeUnschedulableNodes":true}}
- --logtostderr=true
- --v=2
tolerations:
- key: "CriticalAddonsOnly"
operator: "Exists"
serviceAccountName: kube-dns-autoscaler

在文件中,將 <SCALE_TARGET> 替換成擴縮目標。

進入到包含配置文件的目錄中,輸入如下命令創建 Deployment:

kubectl apply -f dns-horizontal-autoscaler.yaml

一個成功的命令輸出是:

deployment.apps/dns-autoscaler created

DNS 水平自動擴縮在已經啟用了。

調優 DNS 自動擴縮參數

驗證 dns-autoscaler ConfigMap 是否存在:

kubectl get configmap --namespace=kube-system

輸出類似於:

NAME                  DATA      AGE
...
dns-autoscaler 1 ...
...

修改該 ConfigMap 中的數據:

kubectl edit configmap dns-autoscaler --namespace=kube-system

找到如下這行內容:

  • *linear: '{"coresPerReplica":256,"min":1,"nodesPerReplica":16}'

根據需要修改對應的字段。“min” 字段表明 DNS 後端的最小數量。 實際後端的數量通過使用如下公式來計算:

replicas = max( ceil( cores × 1/coresPerReplica ) , ceil( nodes × 1/nodesPerReplica ) )

注意 coresPerReplica 和 nodesPerReplica 的值都是浮點數。

背後的思想是,當一個集群使用具有很多核心的節點時,由 coresPerReplica 來控制。 當一個集群使用具有較少核心的節點時,由 nodesPerReplica 來控制。

其它的擴縮模式也是支持的,詳情查看 cluster-proportional-autoscaler

禁用 DNS 水平自動擴縮

有幾個可供調優的 DNS 水平自動擴縮選項。具體使用哪個選項因環境而異。

選項 1:縮容 dns-autoscaler Deployment 至 0 個副本

該選項適用於所有場景。運行如下命令:

kubectl scale deployment --replicas=0 dns-autoscaler --namespace=kube-system

輸出如下所示:

deployment.apps/dns-autoscaler scaled

驗證當前副本數為 0:

kubectl get rs --namespace=kube-system

輸出內容中,在 DESIRED 和 CURRENT 列顯示為 0:

NAME                                 DESIRED   CURRENT   READY   AGE
...
dns-autoscaler-6b59789fc8 0 0 0 ...
...

選項 2:刪除 dns-autoscaler Deployment

如果 dns-autoscaler 為你所控制,也就說沒有人會去重新創建它,可以選擇此選項:

kubectl delete deployment dns-autoscaler --namespace=kube-system

輸出內容如下所示:

deployment.apps "dns-autoscaler" deleted

選項 3:從主控節點刪除 dns-autoscaler 清單文件

如果 dns-autoscaler 在插件管理器 的控制之下,並且具有操作 master 節點的寫權限,可以使用此選項。

登錄到主控節點,刪除對應的清單文件。 dns-autoscaler 對應的路徑一般為:

/etc/kubernetes/addons/dns-horizontal-autoscaler/dns-horizontal-autoscaler.yaml

當清單文件被刪除後,插件管理器將刪除 dns-autoscaler Deployment。

理解 DNS 水平自動擴縮工作原理

  • cluster-proportional-autoscaler 應用獨立於 DNS 服務部署。
  • autoscaler Pod 運行一個客戶端,它通過輪詢 Kubernetes API 服務器獲取集群中節點和核心的數量。
  • 系統會基於當前可調度的節點個數、核心數以及所給的擴縮參數,計算期望的副本數並應用到 DNS 後端。
  • 擴縮參數和數據點會基於一個 ConfigMap 來提供給 autoscaler,它會在每次輪詢時刷新它的參數表, 以與最近期望的擴縮參數保持一致。
  • 擴縮參數是可以被修改的,而且不需要重建或重啟 autoscaler Pod。
  • autoscaler 提供了一個控制器接口來支持兩種控制模式:linear 和 ladder。